package com.xuelang.pipeline.artifact.entry.nexus3v1;

import com.xuelang.pipeline.artifact.app.nexus3v1.ComponentManagementService;
import com.xuelang.pipeline.artifact.app.nexus3v1.response.GetComponentResponse;
import com.xuelang.pipeline.artifact.app.nexus3v1.response.ListComponentResponse;
import com.xuelang.pipeline.artifact.app.nexus3v1.xo.ApiResponseXO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * Nexus3V1 组件管理原生接口web服务
 *
 * @author xiwen.zxg
 * @since 2024/7/9
 */
@RestController
@RequestMapping("/component/management")
public class ComponentManagementController {

    @Autowired
    ComponentManagementService componentManagementService;

    /**
     * 查询组件列表
     *
     * @param continuationToken
     * @param repository
     *
     * @return
     */
    @GetMapping(value = "/list", produces = "application/json;charset=utf-8")
    ApiResponseXO<ListComponentResponse> componentList(
            @RequestParam(value = "continuationToken", required = false) String continuationToken,
            @RequestParam(value = "repository", required = false) String repository) {
        return componentManagementService.componentList(continuationToken, repository);
    }

    /**
     * 上传一个组件
     *
     * @param repository
     * @return
     */
    @PostMapping(value = "/upload", produces = "application/json;charset=utf-8")
    ApiResponseXO<Void> uploadComponent(
            @RequestParam(value = "repository", required = false) String repository,
            @RequestParam(value = "r.asset", required = false) MultipartFile rAsset,
            @RequestParam(value = "r.asset.pathId", required = false) String rAssetPathId,
            @RequestParam(value = "pypi.asset", required = false) MultipartFile pypiAsset,
            @RequestParam(value = "helm.asset", required = false) MultipartFile helmAsset,
            @RequestParam(value = "yum.directory", required = false) String yumDirectory,
            @RequestParam(value = "yum.asset", required = false) MultipartFile yumAsset,
            @RequestParam(value = "yum.asset.filename", required = false) String yumAssetFilename,
            @RequestParam(value = "docker.asset", required = false) MultipartFile dockerAsset,
            @RequestParam(value = "rubygems.asset", required = false) MultipartFile rubygemsAsset,
            @RequestParam(value = "nuget.asset", required = false) MultipartFile nugetAsset,
            @RequestParam(value = "npm.asset", required = false) MultipartFile npmAsset,
            @RequestParam(value = "raw.directory", required = false) String rawDirectory,
            @RequestParam(value = "raw.asset1", required = false) MultipartFile rawAsset1,
            @RequestParam(value = "raw.asset1.filename", required = false) String rawAsset1Filename,
            @RequestParam(value = "raw.asset2", required = false) MultipartFile rawAsset2,
            @RequestParam(value = "raw.asset2.filename", required = false) String rawAsset2Filename,
            @RequestParam(value = "raw.asset3", required = false) MultipartFile rawAsset3,
            @RequestParam(value = "raw.asset3.filename", required = false) String rawAsset3Filename,
            @RequestParam(value = "apt.asset", required = false) MultipartFile aptAsset,
            @RequestParam(value = "maven2.groupId", required = false) String maven2GroupId,
            @RequestParam(value = "maven2.artifactId", required = false) String maven2ArtifactId,
            @RequestParam(value = "maven2.version", required = false) String maven2Version,
            @RequestParam(value = "maven2.generate-pom", required = false) Boolean maven2GeneratePom,
            @RequestParam(value = "maven2.packaging", required = false) String maven2Packaging,
            @RequestParam(value = "maven2.asset1", required = false) MultipartFile maven2Asset1,
            @RequestParam(value = "maven2.asset1.classifier", required = false) String maven2Asset1Classifier,
            @RequestParam(value = "maven2.asset1.extension", required = false) String maven2Asset1Extension,
            @RequestParam(value = "maven2.asset2", required = false) MultipartFile maven2Asset2,
            @RequestParam(value = "maven2.asset2.classifier", required = false) String maven2Asset2Classifier,
            @RequestParam(value = "maven2.asset2.extension", required = false) String maven2Asset2Extension,
            @RequestParam(value = "maven2.asset3", required = false) MultipartFile maven2Asset3,
            @RequestParam(value = "maven2.asset3.classifier", required = false) String maven2Asset3Classifier,
            @RequestParam(value = "maven2.asset3.extension", required = false) String maven2Asset3Extension
    ) {
        return componentManagementService.uploadComponent(repository,
                rAsset, rAssetPathId,
                pypiAsset,
                helmAsset,
                yumDirectory, yumAsset, yumAssetFilename,
                dockerAsset,
                rubygemsAsset,
                nugetAsset,
                npmAsset,
                rawDirectory,
                rawAsset1, rawAsset1Filename,
                rawAsset2, rawAsset2Filename,
                rawAsset3, rawAsset3Filename,
                aptAsset,
                maven2GroupId, maven2ArtifactId, maven2Version, maven2GeneratePom, maven2Packaging,
                maven2Asset1, maven2Asset1Classifier, maven2Asset1Extension,
                maven2Asset2, maven2Asset2Classifier, maven2Asset2Extension,
                maven2Asset3, maven2Asset3Classifier, maven2Asset3Extension);
    }

    /**
     * 得到一个组件信息
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}", produces = "application/json;charset=utf-8")
    ApiResponseXO<GetComponentResponse> getComponent(@PathVariable("id") String id) {
        return componentManagementService.getComponent(id);
    }

    /**
     * 删除一个组件
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}", produces = "application/json;charset=utf-8")
    ApiResponseXO<Void> deleteComponent(@PathVariable("id") String id) {
        return componentManagementService.deleteComponent(id);
    }
}
